
$(document).ready(function(){
    getMepOrderList();
});


function getMepOrderList() {
    $.ajax({
        type: "post",
        url: '/mep/getMepOrderList.do',
        dataType: "json",
        data: {
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==="success"){
                showOrderList(resp.list);
                showWaitSending();
            }else{
                console.log("get_order_message_error!");
            }
        }
    });
}

function showOrderList(list) {

    var html = "";
    for(var i=0;i<list.length;i++){
        var k = 3;
        html = html+" <li style='display: none' class='mep_order "+list[i].order_status+"'>\n" +
            "<div class=\"order_title\">" +
            "<h1>订单号"+list[i].order_no+"</h1>" +
            "<span>"+list[i].create_time+"</span>" +
            "</div>" +
            "<div class=\"order_content\" onclick=\"location.href=ctx+'/mep/toCheckOrder.do?order_no="+list[i].order_no+"'\" >" ;
        if((list[i].order_details).length<k){
            k = (list[i].order_details).length;
        }
        for(var j=0;j<k;j++){
            html = html+ "<div class=\"order_img\"><img src="+list[i].order_details[j].goods_img+" width=\"60\" height=\"40\" /></div>";
        }

        html = html+ "<div class=\"order_price\">" +
            "<h1>共"+list[i].order_details.length+"件商品</h1>" +
            "<p>合计¥"+list[i].order_price_final+"</p>" +
            "<i class=\"iconfont icon-jiantou\"></i>" +
            "</div>" +
            "</div>"

        if(list[i].order_status==='WAITING_ALLOCATION_COURIER'){
            html = html+"<div class=\"order_logistics_btns\">" +
                "<a order_no='"+list[i].order_no+"' onclick='showSetCourier(this)'>选择配送员</a>" +
                "</div>"+"<div class=\"order_logistics_info\">\n" +
                "<h1>店铺名称："+list[i].customer_name+"</h1>\n" +
                "<p>手机号："+list[i].mobile+"</p>\n" +
                "<a href=\"tel:"+list[i].mobile+"\"><img src=\"../images/call_phone.png\" width=\"30\" /></a>\n" +
                "</div>"
        }else{
            html = html+"<div class=\"order_logistics_info\">";

            if(list[i].courier_id===0){
                html = html+ "<h1>配送员：经销商配送</h1></div>";
            }else{
                html = html+"<h1>配送员："+list[i].courier_name+"</h1>\n" +
                    "<p>手机号："+list[i].courier_mobile+"</p>\n" +
                    "<a href=\"tel:"+list[i].courier_mobile+"\"><img src=\"../images/call_phone.png\" width=\"30\" /></a>\n" +
                    "</div>";
            }

            html = html+"<div class=\"order_logistics_info\">\n" +
                "<h1>店铺名称："+list[i].customer_name+"</h1>\n" +
                "<p>手机号："+list[i].mobile+"</p>\n" +
                "<a href=\"tel:"+list[i].mobile+"\"><img src=\"../images/call_phone.png\" width=\"30\" /></a>\n" +
                "</div>"
        }
        html = html+"</li>";
    }
    $('#order_list').html(html);
}


//显示等待配送
function showWaitSending() {
    $(".mep_order").hide();
    $(".order_status").removeClass('onthis');
    $('#status1').addClass('onthis');
    $('.WAITING_ALLOCATION_COURIER').show();
}

//显示等待配送
function showSending() {
    $(".mep_order").hide();
    $(".order_status").removeClass('onthis');
    $('#status2').addClass('onthis');
    $('.WAITING_FOR_DELIVERY').show();
}

//显示已完成订单
function showFinished() {
    $(".mep_order").hide();
    $(".order_status").removeClass('onthis');
    $('#status3').addClass('onthis');
    $('.FINISHED').show();
}

function showSetCourier(obj) {
    $("input[name='chose_shopper']").removeAttr("checked");
    var order_no = $(obj).attr("order_no");
    $('#set_order_no').html(order_no);
    $('#set_order_no_real').val(order_no);
    $('.pop_bg').height($(document).height());
    $('.pop_bg').show();
    $('#chose_shopper_box').show();
    $('#chose_shopper_box2').show();
}

//选择配送员
function setCourier() {
    var courierId = $("input[name='chose_shopper']:checked").val();
    var orderNo = $('#set_order_no_real').val();

    if(checkIsNull(courierId)){
        alertTips("请选择配送员！");
        return false;
    }
    if(confirm("确定分配配送员？")){
        $.ajax({
            type: "post",
            url: '/mep/setCourier.do',
            dataType: "json",
            data: {
                'courier_id':courierId,
                'order_no':orderNo
            },
            success: function(resp){
                console.log(resp);
                if(resp.result==="success"){
                    $('.pop_bg').hide();
                    $('.pop_box').hide();
                    getMepOrderList();
                }else{
                    console.log("SetCourierError!");
                }
            }
        });
    }
}